package a66weding.com.jiehuntong.activity.me;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.PopupWindow;

import java.util.HashMap;
import java.util.Map;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.User;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

/**
 * @version :
 * @creator : AxDu
 * @create-time : 2016/6/29
 * @description :
 */
public class MeBaseActivity extends SwipeBackActivity {

    User user;
    protected String mobileNum;
    protected static final int REALNAME=20;
    protected static final String INTENTTO="intentTo";
    private boolean islogin;

    protected static final int HAVEDATA=1;
    protected static final int NODATA=0;
    protected static final int DATAERROR=2;
    protected static final int LOADINGDATA=3;
    protected static final int NONET=4;
    protected static final int EMPTYDATA=5;
    protected int memberID;

    protected PopupWindow mCommitpop;

    protected Map<String,Activity> tuihuanActivityMap = new HashMap<String,Activity>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        islogin = ((GlobalApplication) getApplication()).isLogin();
        if(islogin) {
            user = DataWareHourse.getInstance().getmUser();
            mobileNum = user.getData().get(0).getNvc_MobilePhone();
            memberID = user.getData().get(0).getI_Id();
        }

    }




    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            if (isHideInput(view, ev)) {
                HideSoftInput(view.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }
    // 判定是否需要隐藏
    private boolean isHideInput(View v, MotionEvent ev) {
        if (v != null && (v instanceof EditText)) {
            int[] l = { 0, 0 };
            v.getLocationInWindow(l);
            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
                    + v.getWidth();
            if (ev.getX() > left && ev.getX() < right && ev.getY() > top
                    && ev.getY() < bottom) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }


    // 隐藏软键盘
    private void HideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(token,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }



    protected void refreshViewLay(int state,View mContentLay,View mProgressLay,View mNonetLay){
        switch (state) {
            case HAVEDATA:
                mProgressLay.setVisibility(View.GONE);
                if(mContentLay.isShown()) {
                    return;
                }else{
                    mContentLay.setVisibility(View.VISIBLE);
                }
                mProgressLay.setVisibility(View.GONE);
                mNonetLay.setVisibility(View.GONE);
                break;
            case NODATA:
                mProgressLay.setVisibility(View.VISIBLE);
                mContentLay.setVisibility(View.GONE);
                mNonetLay.setVisibility(View.GONE);
                break;
            case NONET:
                mProgressLay.setVisibility(View.GONE);
                mContentLay.setVisibility(View.GONE);
                mNonetLay.setVisibility(View.VISIBLE);
                break;
        }
    }



    public void showCommitPopupWindow(Activity activity, View view) {
        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(activity).inflate(
                R.layout.pop_tools_wedregister_commit, null);

        mCommitpop = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);


        //点击空白处时，隐藏掉pop窗口
        mCommitpop.setTouchable(true);

        contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCommitpop.dismiss();
            }
        });


        //设置位置
        mCommitpop.showAtLocation(view, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL,0,0);//设置在屏幕中的显示位置

        // 设置好参数之后再show
        mCommitpop.showAsDropDown(view);

    }


    public Map<String, Activity> getTuihuanActivityMap() {
        return tuihuanActivityMap;
    }

    public void setTuihuanActivityMap(Map<String, Activity> tuihuanActivityMap) {
        this.tuihuanActivityMap = tuihuanActivityMap;
    }
}
